package com.quectel.core.module.mobileuser.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.quectel.constant.global.SqlConstants;
import com.quectel.constant.global.SystemConstants;
import com.quectel.core.constants.RedisCacheConstants;
import com.quectel.core.module.household.dto.HouseholdDto;
import com.quectel.core.module.household.service.HouseholdService;
import com.quectel.core.module.mobileuser.dao.MobileUserDao;
import com.quectel.core.module.mobileuser.dto.MobileUserDto;
import com.quectel.core.module.mobileuser.entity.MobileUserEntity;
import com.quectel.core.module.mobileuser.service.MobileUserService;
import com.quectel.util.common.CopyUtils;
import com.quectel.util.kit.CacheKit;
import com.quectel.util.kit.Snowflake;
import com.quectel.util.mybatis.MyBatisPlusUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * @author caolu
 * @email louis.cao@quectel.com
 * @date 2021-12-14 14:24:08
 */
@DubboService
public class MobileUserServiceImpl implements MobileUserService {

    private static final Logger LOGGER = LoggerFactory.getLogger(MobileUserServiceImpl.class);

    private static final String MOBILE_USER_CACHE_BY_ID = RedisCacheConstants.PROJECT_NAME + "MOBILE_USER_CACHE_BY_ID:";

    @Autowired
    private MobileUserDao mobileUserDao;
    @Autowired
    private HouseholdService householdService;


    @Override
    public List<Long> queryMobilePerson(String name) {
        return mobileUserDao.queryMobilePerson(name);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void editMobile(MobileUserDto mobileUserDto) {
        //修改用户
        MobileUserEntity mobileUserEntity = new MobileUserEntity();
        mobileUserEntity.setId(mobileUserDto.getId());
        mobileUserEntity.setMobile(mobileUserDto.getMobile());
        mobileUserDao.updateById(mobileUserEntity);
        //修改住户
        List<HouseholdDto> householdDtos = householdService.selectByMobile(mobileUserDto.getMobile());

        if (householdDtos != null) {
            for (HouseholdDto householdDto : householdDtos) {
                HouseholdDto updateObj = new HouseholdDto();
                updateObj.setId(householdDto.getId());
                updateObj.setMobile(mobileUserDto.getMobile());
                householdService.updateById(updateObj);
            }
        }

    }

    @Override
    public MobileUserDto selectByMobile(String mobile) {
        MobileUserEntity entity = mobileUserDao.selectOne(new LambdaQueryWrapper<MobileUserEntity>()
                .eq(MobileUserEntity::getMobile, mobile)
                .last(SqlConstants.MYSQL_LIMIT_ONE));
        return paddingField(CopyUtils.copyObj(entity, MobileUserDto.class));
    }

    @Override
    public MobileUserDto selectCacheById(Long id) {
        return CacheKit.cacheToRedis(
                () -> selectById(id),
                MOBILE_USER_CACHE_BY_ID + id,

                SystemConstants.NOT_NULL_CACHE_EXPIRE_SECONDS
        );
    }

    @Override
    public MobileUserDto selectById(Long id) {
        MobileUserEntity entity = mobileUserDao.selectById(id);
        return paddingField(CopyUtils.copyObj(entity, MobileUserDto.class));
    }

    @Override
    public List<MobileUserDto> queryList(Map<String, Object> params) {
        List<MobileUserEntity> list = mobileUserDao.queryList(params);
        List<MobileUserDto> result = CopyUtils.copyList(list, MobileUserDto.class);
        result.forEach(this::paddingField);
        return result;
    }

    private MobileUserDto paddingField(MobileUserDto dto) {
        if (dto != null) {

        }
        return dto;
    }

    @Override
    public int queryTotal(Map<String, Object> params) {
        return mobileUserDao.queryTotal(params);
    }

    @Override
    public Long save(MobileUserDto dto) {
        MobileUserEntity entity = CopyUtils.copyObj(dto, MobileUserEntity.class);
        entity.setId(Snowflake.nextId());
        mobileUserDao.insert(entity);
        return entity.getId();
    }

    @Override
    public void updateById(MobileUserDto dto) {
        MobileUserEntity entity = CopyUtils.copyObj(dto, MobileUserEntity.class);
        mobileUserDao.updateById(entity);
        CacheKit.invalidRedisCache(MOBILE_USER_CACHE_BY_ID + dto.getId());
    }

    @Override
    public void updateAllColumnById(MobileUserDto dto) {
        MobileUserEntity entity = CopyUtils.copyObj(dto, MobileUserEntity.class);


        MyBatisPlusUtils.updateAllColumnById(entity, mobileUserDao);

        CacheKit.invalidRedisCache(MOBILE_USER_CACHE_BY_ID + dto.getId());
    }

    @Override
    public void deleteById(Long id) {
        mobileUserDao.deleteById(id);

        CacheKit.invalidRedisCache(MOBILE_USER_CACHE_BY_ID + id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBatch(Long[] ids) {
        for (Long id : ids) {
            deleteById(id);
        }
    }
}
